import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import model.UserInfo;

public class Main
{
    private static final String getPostDataTime = "8:58:00";
    private static final String executeTime = "8:59:59";
    private static final long nextTime = 250;

    public static void main(String[] args)
    {
            List<UserInfo> itemList = ExcelUtil.getBookItem();
            LogUtil.getInstance().writeLog("main:" + itemList.size());
            addTask(itemList);
            /*
             * for (BookItem item : itemArr) { if
             * (item.getGovernmentId().startsWith("2101041982")) {
             * item.setProductName("iPhone"); } // addTask(item); //
             * bookbook(item); }
             */
    }

    /*
     * private static void bookbook(BookItem item) { Map<String, String>
     * postData = RunTask.getPostData(item); RunTask.book(postData,
     * item.getGovernmentId()); }
     */

    private static void addTask(List<UserInfo> itemList)
    {
        final ScheduledExecutorService scheduledExecutor = Executors
                .newSingleThreadScheduledExecutor();
        long periodTime = 86400000;
        long initialDelay = 0;
        try
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = dateFormat.parse(dayFormat.format(new Date())
                    + " " + getPostDataTime);
            initialDelay = startTime.getTime() - new Date().getTime();
            if (initialDelay < 0)
            {
                initialDelay = periodTime + initialDelay;
            }
            scheduledExecutor.scheduleAtFixedRate(new GetPostData(itemList),
                    initialDelay, periodTime, TimeUnit.MILLISECONDS);
        }
        catch (ParseException e)
        {
        }
    }

    private static class GetPostData extends TimerTask
    {
        List<UserInfo> itemList;

        public GetPostData(List<UserInfo> itemList)
        {
            this.itemList = itemList;
        }

        @Override
        public void run()
        {
            List<Map<String, String>> postDataList = new ArrayList<Map<String, String>>();
            for (UserInfo item : itemList)
            {
                LogUtil.getInstance().writeLog("start getPostData:" + item.getGovernmentId());
		        Map<String, String> postData = PostDataMgr.getInstance().getPostData(item);
                LogUtil.getInstance().writeLog("end getPostData:" + item.getGovernmentId());
                postDataList.add(postData);
            }
            final ScheduledExecutorService scheduledExecutor = Executors
                    .newSingleThreadScheduledExecutor();
            long periodTime = 43200000;
            long initialDelay = 0;
            try
            {
                DateFormat dateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                DateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date startTime = dateFormat.parse(dayFormat.format(new Date())
                        + " " + executeTime);
                initialDelay = startTime.getTime() - new Date().getTime();
                if (initialDelay < 0)
                {
                    initialDelay = periodTime + initialDelay;
                }
                scheduledExecutor.scheduleAtFixedRate(new Book(postDataList),
                        initialDelay, periodTime, TimeUnit.MILLISECONDS);
            }
            catch (ParseException e)
            {
            }
        }
    }

    private static class Book extends TimerTask
    {
        List<Map<String, String>> postDataList;
        int count = 0;

        public Book(List<Map<String, String>> postDataList)
        {
            this.postDataList = postDataList;
        }

        @Override
        public void run()
        {
            for (Map<String, String> postData : postDataList)
            {
                String governmentId = postData.get("governmentId");
                LogUtil.getInstance().writeLog("run book:" + governmentId);
                runThread(postData, governmentId);
                count++;
                
                try
                {
                    if (count < 11)
                    {
	                    Thread.sleep(100);
                    }
                    else
                    {
	                    Thread.sleep(nextTime);
                    }
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
        
        public void runThread(final Map<String, String> postData, final String governmentId)
        {
            new Thread()
            {
                public void run()
                {
                    RunTask.book(postData, governmentId);
                }
            }.start();
        }
    }
}
